<?php

 require_once('bugs/core.php');
 require_once('inc/error.php');

 if (get_magic_quotes_gpc()) {
  foreach ($_REQUEST as $k => $v) { $_REQUEST[$k] = stripslashes($v); }
 }

 // Log us in to the bug tracker
 auth_attempt_script_login('Error Reporter');

 // Disallowed version regexps
 $badversions = array('Unknown', '0\.5(rc|b|\()', '0\.5\.1(rc|b)');

 $sql = 'SELECT version FROM `mantis_project_version_table` WHERE `released` = 0 ORDER BY `date_order` LIMIT 0,1';
 $res = mysql_query($sql);
 define('NEXTVERSION', mysql_result($res, 0));

 // Messages to send
 $messages['fixed'] = 'This error has already been fixed by DMDirc developers. Please consider updating your client.';
 $messages['invalid'] = 'This error has been marked as "invalid" by DMDirc developers.';
 $messages['dupe'] = 'This error has been previously reported, but there is not currently a known fix for it.';
 $messages['outofdate'] = 'Error report invalid: we are no longer accepting error reports from your client version. Please update to a more recent version.';
 $messages['success'] = 'Error report submitted. Thank you.';

 if (isset($_REQUEST['message']) && isset($_REQUEST['trace'])) {
  $message = $_REQUEST['message'];
  $trace   = $_REQUEST['trace'];
  $ip      = $_SERVER['REMOTE_ADDR'];
  $version = isset($_REQUEST['version']) ? $_REQUEST['version'] : 'Unknown';
  $status  = 'new';
  $category = '*Unsorted';

  // Check to see if the version is bad
  foreach ($badversions as $badversion) {
   if (preg_match('(' . $badversion . ')i', $version)) {
    echo $messages['outofdate'], "\n", $messages['success'], "\n";
    exit;
   }
  }

  // Format the trace sanely
  if (!isset($_GET['noformat'])) {
   $trace = substr($_REQUEST['trace'], 1, -1);
   $trace = str_replace(', ', "\n", $trace);
  }

  $source = '';

  // Determine the source of the error
  foreach (explode("\n", $trace) as $line) {
   if (strpos($line, 'com.dmdirc') === 0 && strpos($line, '__CLR2') === false
       && strpos($line, 'com.dmdirc.logger.Logger.assert') !== 0) {
    $source = substr($line, 11);
    break;
   }
  }

  foreach ($cats as $key => $value) {
   if (substr($source, 0, strlen($key)) == $key) {
    $category = $value;
    break;
   }
  }

  if ($category == '*Unsorted' && ctype_upper($source[0])) {
   $category = 'Core';
  }

  // Construct a signature
  $sig = md5($message . $source);

  $sql = 'SELECT issue_id FROM errors WHERE error_signature = \'' . $sig . '\'';
  $res = mysql_query($sql);

  if (mysql_num_rows($res) == 0) {
   $bugdata = new BugData();
   $bugdata->project_id = 1;
   $bugdata->reporter_id = 10;
   $bugdata->build = $version;
   $bugdata->view_state = VS_PRIVATE;
   $bugdata->summary = $message . ' — ' . $source;
   $bugdata->description = $trace;
   $bugdata->category = $category; 
   $bugdata->target_version = NEXTVERSION;

   $id = bug_create($bugdata);

   bugnote_add($id, 'Originally reported by ' . $ip, '0:00', true);

   $sql  = 'INSERT INTO errors (issue_id, error_signature) VALUES (';
   $sql .= $id . ', \'' . $sig . '\')';
   mysql_query($sql);
  } else {
   $bugid = mysql_result($res, 0);

   $sql = 'SELECT resolution FROM mantis_bug_table WHERE id = ' . $bugid;
   $res = mysql_query($sql);
   $resolution = mysql_result($res, 0);

   $cont = true;

   do {
    $sql = 'SELECT destination_bug_id, resolution FROM mantis_bug_relationship_table INNER JOIN mantis_bug_table ON mantis_bug_relationship_table.destination_bug_id = mantis_bug_table.id WHERE relationship_type = 0 AND source_bug_id = ' . $bugid;
    $res = mysql_query($sql);
    
    if (mysql_num_rows($res) > 0) {
     $row = mysql_fetch_assoc($res);
     $bugid = $row['destination_bug_id'];
     $resolution = $row['resolution'];
    } else {
     $cont = false;
    }
   } while ($cont);

   bugnote_add($bugid, 'Also reported by ' . $ip . ' (version: ' . trim($version) . ')', '0:00', true);

   switch ((int) $resolution) {
    case OPEN:
    case REOPENED:
    case SUSPENDED:
     $status = 'dupe';
     break;
    case FIXED:
     $status = 'fixed';
     break;
    case UNABLE_TO_DUPLICATE:
    case NOT_FIXABLE:
    case DUPLICATE:
    case NOT_A_BUG:
    case WONT_FIX:
     $status = 'invalid';
     break; 
   }

   echo $messages[$status] . "\n";
  }

  echo $messages['success'] . "\n";

 } else {
  die('Submission failed: invalid parameters');
 }

?>
